<!--
Copyright (c) 2009 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>WebGL ActiveTexture BindTexture conformance test #2</title>
    <link rel="stylesheet" href="../resources/js-test-style.css"/>
    <script src="../resources/js-test-pre.js"></script>
    <script src="resources/webgl-test.js"> </script>
</head>
<body>
<canvas id="example" width="2" height="2" style="width: 40px; height: 40px;"></canvas>
<canvas id="canvas2d" width="1" height="1" style="width: 40px; height: 40px;"></canvas>
<div id="description"></div>
<div id="console"></div>
<script id="vshader" type="x-shader/x-vertex">
uniform mat4 world;
attribute vec3 vPosition;
attribute vec2 texCoord0;
varying vec2 texCoord;
void main()
{
  gl_Position = world * vec4(vPosition, 1);
  texCoord = texCoord0;
}
</script>
<script id="fshader2d" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D tex2d;
varying vec2 texCoord;
void main()
{
  gl_FragColor = texture2D(tex2d, texCoord);
}
</script>
<script id="fshaderCube" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
uniform samplerCube texCube;
void main()
{
  gl_FragColor = textureCube(texCube, vec3(0,1,0));
}
</script>

<script>
function init()
{
  if (window.initNonKhronosFramework) {
    window.initNonKhronosFramework(false);
  }

  debug("Tests that binding both TEXTURE_2D and TEXTURE_CUBE_MAP to the same");
  debug("active texture unit works as long as they are not used");
  debug("simultaneously in the same shader program.");
  debug("");

  var canvas2d = document.getElementById("canvas2d");
  var ctx2d = canvas2d.getContext("2d");
  ctx2d.globalCompositeOperation = "copy";

  gl = initWebGL("example", "vshader", "fshader2d", ["vPosition", "texCoord0"],
                 [ 0, 0, 0, 1 ], 1);

  var program2d = gl.program;
  var programCube = createProgram(
      gl, "vshader", "fshaderCube", ["vPosition", "texCoord0"]);

  gl.disable(gl.DEPTH_TEST);
  gl.disable(gl.BLEND);

  var vertexObject = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
  gl.bufferData(
      gl.ARRAY_BUFFER,
      new Float32Array([-1, 1,0, 1,1,0, -1,-1,0,
                        -1,-1,0, 1,1,0,  1,-1,0]),
      gl.STATIC_DRAW);
  gl.enableVertexAttribArray(0);
  gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);

  var vertexObject = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
  gl.bufferData(
      gl.ARRAY_BUFFER,
      new Float32Array([ 0,0, 1,0, 0,1,
                         0,1, 1,0, 1,1]),
      gl.STATIC_DRAW);
  gl.enableVertexAttribArray(1);
  gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 0, 0);

  // Make texture unit 1 active.
  gl.activeTexture(gl.TEXTURE1);

  // Make a 2d texture
  var tex2d = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, tex2d);
  ctx2d.fillStyle = "rgba(0, 0, 255, 255)";
  ctx2d.fillRect(0, 0, 1, 1);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d);

  // make a cube texture
  var texCube = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_CUBE_MAP, texCube);
  ctx2d.fillStyle = "rgba(255, 0, 255, 255)";
  ctx2d.fillRect(0, 0, 1, 1);
  var targets = [
    gl.TEXTURE_CUBE_MAP_POSITIVE_X,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
    gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
    gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
    gl.TEXTURE_CUBE_MAP_NEGATIVE_Z];
  for (var ii = 0; ii < targets.length; ++ii) {
    gl.texImage2D(targets[ii], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas2d);
  }

  // Setup program2d and programCube
  var tex2dLoc = gl.getUniformLocation(program2d, "tex2d");
  var world2dLoc = gl.getUniformLocation(program2d, "world");
  var texCubeLoc = gl.getUniformLocation(programCube, "texCube");
  var worldCubeLoc = gl.getUniformLocation(programCube, "world");

  gl.useProgram(program2d);
  gl.uniform1i(tex2dLoc, 1);
  gl.useProgram(programCube);
  gl.uniform1i(texCubeLoc, 1);

  gl.clearColor(1,0,0,1);
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

  var programs = [program2d, programCube];
  var worldLocs = [world2dLoc, worldCubeLoc];
  for (var ii = 0; ii < 4; ++ii) {
    var x = ii % 2;
    var y = Math.floor(ii / 2);
    gl.useProgram(programs[x]);
    gl.uniformMatrix4fv(
        worldLocs[x], false,
        [0.5, 0, 0, 0,
         0, 0.5, 0, 0,
         0, 0, 1, 0,
         -0.5 + x, -0.5 + y, 0, 1]);
    gl.drawArrays(gl.TRIANGLES, 0, 6);
  }

  var colors = [
      [0,0,255,255],
      [255,0,255,255],
      [0,0,255,255],
      [255,0,255,255]];

  for (var ii = 0; ii < colors.length; ++ii) {
    var c = colors[ii];
    var x = ii % 2;
    var y = Math.floor(ii / 2);
    var buf = new Uint8Array(4);
    gl.readPixels(x, y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    var msg = 'expected:' +
        c[0] + ', ' + c[1] + ', ' + c[2] + ', ' + c[3] + ' found: ' +
        buf[0] + ', ' +
        buf[1] + ', ' +
        buf[2] + ', ' +
        buf[3];
    if (buf[0] != c[0] ||
        buf[1] != c[1] ||
        buf[2] != c[2] ||
        buf[3] != c[3]) {
      testFailed(msg);
      return;
    }

    testPassed(msg);
  }
}

init();
successfullyParsed = true;
</script>
<script src="../resources/js-test-post.js"></script>

</body>
</html>

